﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DLULearningNetworkModel;
using DLULearningNetwork.ViewModels;
using DLULearningNetwork.Libs;
using DLULearningNetwork.ActionFilterAttributes;
using System.Web.Helpers;
namespace DLULearningNetwork.Controllers
{
    public class PrivacyController : Controller
    {
        [RequireUserLogin()]
        public ActionResult Index()
        {
            //Undone : truong hop luc dang ky, he thong bi hu, chua tao tap privacyflag mac dinh
            List<PrivacySetttingViewModel> lstPrivacySettingViewModel = new List<PrivacySetttingViewModel>();
            Account curUser = RequireUserLogin.LoginUser;
            Profile profileOfCurUser = Profile.Single(curUser.AccountID_u);
            List<PrivacyFlag> lstPrivacyFlag = PrivacyFlag.SelectByProfileID(profileOfCurUser.ProfileID_u);
            //Tam thoi xu ly la tu tao moi luc nay, tat ca la mac dinh, che do private
            if (lstPrivacyFlag.Count ==0)
            {
                PrivacyFlag.CreatePrivacyFlagDefault(profileOfCurUser.ProfileID_u);
                lstPrivacyFlag = PrivacyFlag.SelectByProfileID(profileOfCurUser.ProfileID_u);
            }
            List<VisibilityLevel> lstVisibilityLevel = VisibilityLevel.GetAll();
            PrivacySetttingViewModel privacySettingViewModel = null;
            foreach (PrivacyFlag privacyFlag in lstPrivacyFlag)
            {
                privacySettingViewModel = new PrivacySetttingViewModel();
                privacySettingViewModel.PrivacyFlagID_u = privacyFlag.PrivacyFlagID_u.ToString();
                privacySettingViewModel.DisplayFieldName = privacyFlag.PrivacyFlagTypeReference.Value.DisplayedFieldName_s;
                privacySettingViewModel.PrivacyFlagTypeID = privacyFlag.PrivacyFlagTypeID_u.ToString();
                privacySettingViewModel.SortOrder = privacyFlag.PrivacyFlagTypeReference.Value.SortOrder_i;
                privacySettingViewModel.VisibilityLevelID = privacyFlag.VisibilityLevelID_u.ToString();
                lstPrivacySettingViewModel.Add(privacySettingViewModel);
            }
            ViewBag.LstVisibilityLevel = lstVisibilityLevel;
            lstPrivacySettingViewModel.OrderBy(m => m.SortOrder);
            return View(lstPrivacySettingViewModel);
        }

        [HttpPost]
        [RequireUserLogin()]
        public string SaveSetting(PrivacySetttingViewModel[] arrayPrivacySetting)
        {
            try
            {
                //Lay current user in Session
                Account currentUser = (Account)LibSession.Get(Constants.ACCOUNT_LOGIN);
                Profile profile = Profile.Single(currentUser.AccountID_u);
                List<PrivacyFlag> lstPrivacyFlag = new List<PrivacyFlag>();
                PrivacyFlag privacy = null;
                foreach (PrivacySetttingViewModel privacySettingVM in arrayPrivacySetting)
                {
                    privacy = new PrivacyFlag();
                    privacy.CreateDate_dt = DateTime.Now;
                    privacy.PrivacyFlagID_u = Guid.Parse(privacySettingVM.PrivacyFlagID_u);
                    privacy.PrivacyFlagTypeID_u = Guid.Parse(privacySettingVM.PrivacyFlagTypeID);
                    privacy.ProfileID_u = profile.ProfileID_u;
                    privacy.VisibilityLevelID_u = Guid.Parse(privacySettingVM.VisibilityLevelID);
                    lstPrivacyFlag.Add(privacy);
                }
                if (PrivacyFlag.Update(lstPrivacyFlag) > 0)
                {
                    return "1";
                }
                return "Có lỗi, vui lòng thử lại sau";
            }
            catch {
                return "Có lỗi, vui lòng thử lại sau";
            }
        }
    }
}
